<template>
	<view class="">
		<u-sticky><tab :tab-list="productCate" :allShow="false" @contTabChange="contTabChange"></tab></u-sticky>
		<view class="container">
			<view class="" v-if="list.length == 0">
				<view class="p-2 mt-3"><u-empty mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png"></u-empty></view>
			</view>
			<view v-else style="margin-bottom: 90rpx;">
				<view>
					<product-list
						v-if="list.length >= 1"
						:list="list"
						select="true"
						:btn-show="false"
						:check-show="true"
						@proDel="proDel"
						class="flex flex-wrap justify-between"
						:exhibition-id="exhibitionId"
					></product-list>
				</view>
				<u-loadmore :status="status" color="#E5E5E5" lineColor="#E5E5E5" dashed line />
			</view>
		</view>
		<button class="submit_check bg_main fixed-bottom text-white font" @click="checkSubmit">
			确认选择
			<text v-if="checkNum >= 1">( {{ checkNum }} )</text>
		</button>
	</view>
</template>

<script>
import tab from '@/components/tab/index.vue';
import productList from '@/components/product_list/product_list.vue';
import { productCategory, getProductList } from '@/api/home/nav/product_data.js';
export default {
	components: { tab, productList },
	data() {
		return {
			productCate: [], //分类
			status: 'loadmore',
			page: 1, //页数
			suo: true,
			catId: '',
			list: [],
			checkNum: 0,
			exhibitionId: ''
		};
	},
	onLoad(option) {
		this.exhibitionId = option.exhibitionId;
		this.getInfo();
		// uni.removeStorageSync('checkList');
		// 分享
		wx.showShareMenu({
			withShareTicket: true,
			menus: ['shareAppMessage', 'shareTimeline']
		});
		this.proDel();
	},
	onReachBottom() {
		if (this.suo) {
			//定义个小锁，当没有数据时锁住suo
			this.status = 'loading';
			this.page = ++this.page;
			this.getProduct(this.catId);
		}
	},
	methods: {
		reset() {
			this.list = [];
			this.page = 1;
		},
		getInfo() {
			// 产品分类
			productCategory({ zhanhui_id: this.exhibitionId })
				.then(res => {
					if (res.code == 0) {
						this.productCate = res.data;
						this.getProduct(res.data[0].id || '');
					} else {
						uni.$u.toast(res.msg);
						this.getProduct('');
					}
				})
				.catch(err => {
					console.log('获取产品分类失败', err);
				});
		},
		contTabChange(e) {
			// console.log(val);
			this.catId = e.val.id;
			this.reset();
			this.getProduct(e.val.id);
		},
		// 产品列表
		getProduct(val) {
			let res = {
				page: this.page,
				mid: val,
				kw: this.searchVal || '',
				token: uni.getStorageSync('userInfo').token || '',
				uid: uni.getStorageSync('userInfo').userId || '',
				zhanhui_id: this.exhibitionId
			};
			getProductList(res).then(res => {
				// console.log('产品列表', res.data.list);
				if (res.data.list.length >= 1) {
					this.list.push(...res.data.list);
					if (res.data.list.length == 0) {
						this.status = 'nomore';
						this.suo = false;
					} else if (res.data.list.length < 10) {
						this.status = 'nomore';
						this.suo = false;
					} else {
						this.status = 'loadmore';
						this.suo = true;
					}
				} else {
					uni.showModal({
						title: '提示',
						content: '您认证的企业还未发布产品，快去发布一些再来参展吧！',
						showCancel: true,
						cancelText: '取消',
						confirmText: '去发布',
						success: res => {
							if (res.confirm) {
								uni.navigateTo({
									url: '/subPages/my/company_main/index'
								});
							} else {
								return;
							}
						}
					});
				}
			});
		},
		proDel() {
			let checkList = [...getApp().globalData.pro1, ...getApp().globalData.pro2];
			// console.log("checkList==>",checkList);
			this.checkNum = checkList.length;
		},
		// 确认选择
		checkSubmit() {
			if (this.checkNum <= 0) {
				uni.$u.toast('请选择参展产品');
			} else {
				uni.navigateBack({
					delta: 1
				});
			}
		}
	}
};
</script>

<style lang="scss" scoped>
.submit_check {
	width: 100%;
	height: 90rpx;
	line-height: 90rpx;
}
</style>
